//
//  TestTimer.m
//  TestOfTimer
//
//  Created by 阳兵青 on 16/1/6.
//  Copyright © 2016年 ouyang. All rights reserved.
//

#import "TestTimer.h"
#define GetCodeTickerCount 60
@interface TestTimer ()
{
    dispatch_source_t _makeTimer;
    dispatch_source_t _clickTimer;
    dispatch_queue_t _queue;
}
@end

@implementation TestTimer
//创建定时器
- (void)makeTimerClickButton:(UIButton *)clickButton clickLabel:(UILabel *)clickLabel addressStr:(NSString *)addressStr {
    //归档当前时间
    NSDate *date = [NSDate date];
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [document stringByAppendingPathComponent:addressStr];
    [NSKeyedArchiver archiveRootObject:date toFile:path];
    //iOS提供的读秒方法
    clickButton.userInteractionEnabled = NO;
    __block int timeout = GetCodeTickerCount;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _makeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_makeTimer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(_makeTimer, ^{
        if (timeout <= 0) {
            dispatch_source_cancel(_makeTimer);
            dispatch_async(dispatch_get_main_queue(), ^{
                clickButton.userInteractionEnabled = YES;
                clickLabel.text = @"获取动态码";
                clickLabel.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:137 / 255.0 blue:225 / 255.0 alpha:1];
            });
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                clickLabel.text = [NSString stringWithFormat:@"%d秒后重新发送",timeout] ;
            });
            timeout--;
        }
    });
    dispatch_resume(_makeTimer);
}

//检查定时器
- (void)clickTimerClickButton:(UIButton *)clickButton clickLabel:(UILabel *)clickLabel addressStr:(NSString *)addressStr {
    //时间检测,解档
    NSDate *currentTime = [NSDate date];
    //解档
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [document stringByAppendingPathComponent:addressStr];
    NSDate *beforeTime = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    //时间差
    NSTimeInterval interval = [currentTime timeIntervalSinceDate:beforeTime];
    if (beforeTime != nil && interval < GetCodeTickerCount) {
        clickButton.userInteractionEnabled = NO;
        __block int timeout = GetCodeTickerCount - interval + 1;
        _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        _clickTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
        dispatch_source_set_timer(_clickTimer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
        dispatch_source_set_event_handler(_clickTimer, ^{
            if (timeout <= 0) {
                dispatch_source_cancel(_clickTimer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    clickButton.userInteractionEnabled = YES;
                    clickLabel.text = @"获取动态码";
                    clickLabel.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:137 / 255.0 blue:225 / 255.0 alpha:1];
                });
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    clickLabel.text = [NSString stringWithFormat:@"%d秒后重新发送",timeout];
                });
                timeout--;
            }
        });
        dispatch_resume(_clickTimer);
    }
}

//结束定时器
- (void)endTimerClickButton:(UIButton *)clickButton clickLabel:(UILabel *)clickLabel addressStr:(NSString *)addressStr {
    dispatch_suspend(_makeTimer);
    dispatch_time_t lastTime = dispatch_time(DISPATCH_TIME_NOW, 600 * NSEC_PER_SEC);
    dispatch_after(lastTime, _queue, ^{
        dispatch_resume(_makeTimer);
    });
    clickButton.userInteractionEnabled = YES;
    clickLabel.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:137 / 255.0 blue:225 / 255.0 alpha:1];
    clickLabel.text = @"获取动态码";
        
    //删除归档文件
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [document stringByAppendingPathComponent:addressStr];
    [fileManager removeItemAtPath:path error:nil];
}
@end
